function [X_dual, dec] = hwk7(phi0, f0, SNR)

Nb = 2000;
Nd = Nb / 2;

T = 1e-3;
%Here, we make each pulse have 10 samples
t = linspace(0,T,10);
B = 2*1/T;

%Create a time vector for plotting
plot_t = 0:T/10:(Nb*T)/2;
plot_t = plot_t(1:end-1);

Ag = 1; %Amplitude of g(t)
g = Ag * ones(1,length(t));

%Noise parameters
%Numerical computation of the infinite
% integral of sinc^2 gives 1...
var_w = sqrt(1 / (10^(SNR/10)));

%Propagation delay parameters
phiE = phi0 + 2*pi*f0*plot_t;

%Create the array of RV's
X = rand(1,Nb) > 0.5;

dn = [];

%Gray codewords
d1 = ( 1 + j)/sqrt(2); %00
d2 = (-1 + j)/sqrt(2); %01
d3 = (-1 - j)/sqrt(2); %11
d4 = ( 1 - j)/sqrt(2); %10

X_dual = [];
%Determine Symbols 
for i=1:2:Nb
  X_dual = [X_dual [X(i) X(i+1)]'];
  if [X(i) X(i+1)] == [0 0]
    dn = [dn d1];
  elseif [X(i) X(i+1)] == [0 1]
    dn = [dn d2];
  elseif [X(i) X(i+1)] == [1 1]
    dn = [dn d3];
  elseif [X(i) X(i+1)] == [1 0]
    dn = [dn d4];
  end
end

u = [];
for i=1:length(dn)
  u = [u dn(i) * g];
end

%Create AWGN with variance var_w that is the same size as u
w = sqrt(var_w)*randn(length(u), 1) + sqrt(var_w)*j*randn(length(u), 1);
w = w';

subplot(4,2,[1 2]);
plot(plot_t, real(u));
title('Re\{u(t)\}');

subplot(4,2,[3 4]);
plot(plot_t, imag(u));
title('Im\{u(t)\}');
xlabel('Time (s)');

%Create the input to the matched filter ("v")
v = u .* exp(j.*phiE) + w;

%yt is the non-sampled output of the matched filter
yt = conv(v, g);
yn = [];

%Sample y(t) every 10 units to create y[n]
for i=10:10:length(yt)
  yn = [yn yt(i)];
end

dec = [];
%Make the decision, with 0 as the threshold
for i=1:length(yn)
  if     (real(yn(i)) > 0 && imag(yn(i)) > 0)
    dec = [dec [0 0]'];
  elseif (real(yn(i)) < 0 && imag(yn(i)) > 0)
    dec = [dec [0 1]'];
  elseif (real(yn(i)) < 0 && imag(yn(i)) < 0)
    dec = [dec [1 1]'];
  elseif (real(yn(i)) > 0 && imag(yn(i)) < 0)
    dec = [dec [1 0]'];
  end
end

subplot(5,2,[7 9]);
scatter(real(v), imag(v));
title(['v(t), \theta_0=', num2str(phi0),', f_0=', num2str(f0), ', SNR=', num2str(SNR)]);
ylabel('Im\{v\}');
xlabel('Re\{v\}');
axis([-3 3 -3 3]);
grid on

subplot(5,2,[8 10]);
scatter(real(yn), imag(yn));
title('y[n] - Sampled output of matched filter');
ylabel('Im\{y[n]\}');
xlabel('Re\{y[n]\}');
axis([-15 15 -15 15]);
grid on
